package cn.wolfcode.luowowo.article.service.impl;


import cn.wolfcode.luowowo.article.domain.Destination;
import cn.wolfcode.luowowo.article.domain.Region;
import cn.wolfcode.luowowo.article.domain.ViewpointAttraction;
import cn.wolfcode.luowowo.article.mapper.DestinationMapper;
import cn.wolfcode.luowowo.article.mapper.RegionMapper;
import cn.wolfcode.luowowo.article.mapper.ViewpointAttractionMapper;
import cn.wolfcode.luowowo.article.service.IDestinationService;
import cn.wolfcode.luowowo.common.query.QueryObject;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@Service
public class DestinationServiceImpl implements IDestinationService {

    @Autowired
    private DestinationMapper mapper;
    @Override
    public PageInfo selectFoList(QueryObject qo) {
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        List<Destination> list = mapper.selectForList(qo);
        return new PageInfo(list);
    }

    @Override
    public List<Destination> listToasts(Long id) {
        List<Destination> list = new ArrayList<>();
        Destination dest = mapper.selectByPrimaryKey(id);
        list.add(dest);
        while (dest.getParent() != null){
            dest = mapper.selectByPrimaryKey(dest.getParent().getId());
            list.add(dest);
        }

        Collections.reverse(list);
        return list;
    }

    @Override
    public AjaxResult changeHot(Destination destination) {
        AjaxResult result = new AjaxResult();
        try {
            mapper.changeHot(destination);
        }catch (Exception e){
            result.mark("操作失败");
        }
        return result;
    }

    @Override
    public List<Destination> listAll() {
        return mapper.selectAll();
    }

    @Autowired
    private RegionMapper regionMapper;
    @Autowired
    private ViewpointAttractionMapper viewpointAttractionMapper;
    @Override
    public List<Destination> listDestByRegionId(Long rid) {
        //获取关联目的地的id
        Region region = regionMapper.selectByPrimaryKey(rid);
        List<Destination> dests = mapper.listDestByRegionId(region.getRefIds());
        return dests;
    }

    @Override
    public List<Destination> listHotDestByRegionId(Long regionId) {
        if (regionId > 0){
           return mapper.listHotDestByRegionId(regionId);
        }else {
            return mapper.selectProvince();
        }
    }

    @Override
    public List<Destination> list() {
        return mapper.selectAll();
    }

    @Override
    public Destination getCountry(Long destId) {
        List<Destination> toasts = listToasts(destId);
        return toasts.get(0);
    }

    @Override
    public Destination getProvince(Long destId) {
        List<Destination> toasts = listToasts(destId);
        return toasts.size() > 1 ? toasts.get(1) : null;
    }

    public Destination selectById(Long refId) {
        return mapper.selectById(refId);
    }



    @Override
    public List<Destination> listByHotelTheme6(String themeName) {
        return mapper.listByHotelTheme6(themeName);
    }

    @Override
    public List<Destination> listDestByRecmmendId(Long[] refIds) {
        return mapper.listDestByRecmmendId(refIds);
    }



    //使用目的地id查目的地景点概况
    public ViewpointAttraction getAttractionByDestId(Long destId) {
        return viewpointAttractionMapper.selectAttractionByDestId(destId);
    }

}
